package zhtt;

import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;

/**
 * Created by zhtt on 2017/4/11.
 */
public class JunitTest_get {

    private Session session;

    @Before
    public void setUp(){
        session=HbnUtil.getSession();
    }

    @Test
    public void get(){
        try{
            session.beginTransaction();
            //查看sql语句，看出是从数据库中加载
            Student student=session.get(Student.class,1);
            System.out.println(student);
            //查看sql语句，从session缓存中加载
            Student student1=session.get(Student.class,1);
            System.out.println(student1);
            //从缓存中清除（session.clear()、）
            session.evict(student);
            //再次获取，从数据库中加载
            Student student2=session.get(Student.class,1);
            System.out.println(student2);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
